package it.struts.helloworld.action;

import it.struts.helloworld.form.OrdineForm;
import it.struts.helloworld.model.Carrello;
import it.struts.helloworld.model.Ordine;
import it.struts.helloworld.services.OrdineService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AddOrderAction extends Action {
	
	/**
	 * Execute method.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance
	 * @param form
	 *            The optional ActionForm bean for this request
	 * @param request
	 *            The servlet request we are processing
	 * @param response
	 *            The servlet response we are creating
	 * 
	 * @exception Exception
	 *                if business logic throws an exception
	 */

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		OrdineForm objForm = (OrdineForm) form;

		// Retrieve the DAO Reference
//		roseindia.dao.ShoppingCartDAO shoppingCartDAO = (roseindia.dao.ShoppingCartDAO) ServiceFinder
//				.getContext(request).getBean(ShoppingCartConstants.DAO_BEAN);
		// By Default show the Add/Edit Page
		String forwardToPage = "input";

		// getting curency symbol
//		String CurrencySymbol = shoppingCartDAO.getCurrencySymbol();
//		request.setAttribute("csymbol", CurrencySymbol);

		HttpSession session = request.getSession();

		try {

			//Utilities utill = new Utilities();

			if (request.getParameter("action").equals("add"))
					//&& objForm.getActionUpdateData().equals("")) 
				{

				// getting seession data

				session.getAttribute("cart");
				double prezzoTot = (Double) session.getAttribute("prezzoTot");
				Long quantTot = (Long)session.getAttribute("quantitaTot");
				Carrello cart = (Carrello) session.getAttribute("cart");

				// Set to the request attribute to show the items in the cart
				request.setAttribute("cartItems", cart.getItems());
				request.setAttribute("prezzoTot", prezzoTot);
				System.out.println(request.getParameter("prezzoTot"));
				request.setAttribute("quantitaTot", quantTot);
		
			// In case of form submit Add/Update the data

					Ordine o = new Ordine();

					// pojoObj.setOrderid(objForm.getOrderId());
//					o.setData(objForm.getData());
//					o.setStato(objForm.getStato());
					o.setPrezzoTot(prezzoTot);
					o.setQuantitaTot(quantTot);

					// Add the data
					OrdineService.newOrdine(o);
				}
					// remove cart from session
					session.invalidate();

					forwardToPage = "success";

		} catch (Exception e) {
			forwardToPage = "input";
			e.printStackTrace();
		}
		// Display the registration form to the user
		return mapping.findForward(forwardToPage);
		
	}


}
